<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>餐饮总部账单管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-form-label{
            width: 120px;
        }
        @media screen and (max-width: 450px){
            .layui-form-item .layui-input-inline {
                margin-left: 150px;
            }
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">账单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="billNo" autocomplete="off" class="layui-input" placeholder="账单编号" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">餐饮总部名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="foodManagerName" autocomplete="off" class="layui-input" placeholder="餐饮总部名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">生成账单时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="selectTime" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">账单状态</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-filter="status">
                                    <option value="">全部</option>
                                    <option value="1">待结账</option>
                                    <option value="2">已付款待确认</option>
                                    <option value="3">已完成</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary reReset" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'laydate'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate;
        var excelList,currPage
		
		$('.reReset').click();

        laydate.render({
            elem: '#selectTime'
            ,theme: '#fbaf7e'
            ,range: "-"
        });

        var selectTable;
        selectTable = http.table({
            elem: '#selectTable',
            url: 'admin/order/bill/listPage/foodManager',
            method: 'get',
            id: 'selectTable',
            toolbar: '#toolbarDemo',
            done: function(res, curr, count){
			    excelList = res.data;
                currPage = curr
			},
            cols: [[
                {title:'序号',type:'numbers',fixed: 'left'}
                ,{field:'billId', title: '账单编号', align : 'center',  width: 235,
                    templet: function(d){
                        var html = ''
                        html += '<a class="buttoncolor" href="billSupplierOrder.html?id='+d.billId+'">'+d.billId+'</a>'
                        return html
                    }
                }
                ,{field:'foodManagerName', title: '餐饮总部名称', align : 'center'}
                ,{field:'totalMoney', title: '金额', align : 'center'}
                ,{field:'status', title: '账单状态', align : 'center',width: 160,
					templet: function (d) {
						return d.status==1?'<span style="color:#0066FF">待结账</span>':(d.status==2?'<span style="color:#FF0000">已付款待确认</span>':(d.status==3?'<span style="color: #000000">已完成</span>':''));
                    }
                }
				,{field:'createTime', title: '生成账单时间', align : 'center',width: 170}
                ,{field:'orderCount', title: '订单数量', align : 'center'}
                ,{field:'billCycle', title: '结账周期', align : 'center',
					templet: function (d) {
                        var html = ''
                        switch (d.billCycle){
                            case 7:
                                html = '7天'
                                break
                            case 15:
                                html = '15天'
                                break
                            case 30:
                                html = '一个月'
                                break
                            case 90:
                                html = '一个季度'
                                break
                            case 182:
                                html = '半年'
                                break
                            case 365:
                                html = '一年'
                                break
                        }
						return html
                    }
                }
                ,{field:'', title: '操作', align : 'center',fixed: 'right',width: 230,
                    templet: function (d) {
                        var html = '';
                        if(d.status == 1){
                            html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="modify" >修改</button>'
                            html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" >删除</button>'

                        }else if(d.status == 2){
                            html += '<button type="button" class="layui-btn layui-btn-xs" lay-event="sure" >确认收款</button>'
                        }
                        return html;
                    }
                }
            ]]
        });

        //查询
        form.on('submit(searchBth)', function (data) {
            let _data = data.field,
				param = _data;
			var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
			var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
			param.startTime = startTime;
			param.endTime = endTime;
			selectTable.reload({
				page:{curr:1},
				where: param
			})
        })

        // table操作
        table.on('tool(selectTable)', function(obj){
            var data = obj.data;
            if(obj.event=='modify'){
                // 修改
                window.location.href = 'billSupplierOrder.html?id=' + data.billId
            }else if(obj.event=='del'){
                // 删除
                http.myOpen({
                    title:'提示',
                    content:'<p style="text-align:center;margin-top:20px">确认删除账单？</p>',
                    area:['300px','150px'],
                    btn:['确定','取消'],
                    btn1:function(index, layero){
                        http.post('admin/order/bill/delete',{
                            id: data.billId
                        },function(res){
                            if(res.code == 200){
                                layer.msg('操作成功')
                                layer.close(index)
                                table.reload('selectTable');
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                        
                    }
                });
            }else if(obj.event=='sure'){
                // 确认收款
                location.href = 'billSupplierOrder.html?id=' + data.billId
            }
        })

        table.on('toolbar(selectTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data; //获取选中行数据
            if(obj.event=='export'){ // 导出
				var data = excelList; 
				if (data.length == 0) {
					layer.msg('当前页没有可导出的数据');
					return;
                }
                var param = {
                    page: currPage,
                    limit: 10,
                    authentication: localStorage.getItem('xi-can-management-authentication'),
                    startTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'',
                    endTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'',
                    status: $('select[name=status]').val(),
                    billNo: $('input[name=billNo]').val(),
                    foodManagerName: $('input[name=foodManagerName]').val(),
                }
                if (param) {  
                    let paramsArray = [];  
                    //encodeURIComponent  
                    Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                    window.open(TUrl+"admin/order/bill/export/foodManager?"+paramsArray.join('&'))
                }
			}
        })

    })

</script>
</html>